<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>后台登录</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon"/>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/css/font.css">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/css/xadmin.css">
    <script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/static/lib/layui/layui.js"
            charset="utf-8"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/static/js/xadmin.js"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->

    <style>
        .btn {
            border: none;
            width: 100%;
            height: 50px;
            outline: 0px;
            font-size: 16px;
            font-weight: 500;
            cursor: pointer;
        }

        #verifyBtn {
            font-size: 14px;
            margin-left: 10px;
            margin-top: 3px;
            height: 45px;
        }
    </style>
</head>
<body class="login-bg">

<div class="login layui-anim layui-anim-up">
    <div class="message">手机号登录</div>
    <div id="darkbannerwrap"></div>

    <form method="post" class="layui-form" onsubmit="return false">
        <input name="userPhone" placeholder="请输入手机号" maxlength="11" type="text" lay-verify="phone" class="layui-input">
        <hr class="hr15">
        <div class="layui-row">
            <div class="layui-col-md6">
                <input name="verifyCode" lay-verify="required" maxlength="4" placeholder="请输入验证码" type="text"
                       class="layui-input">
            </div>
            <div class="layui-col-md6">
                <button class="layui-btn feachBtn layui-btn-lg layui-btn-normal" id="verifyBtn">点击获取验证码</button>
            </div>
        </div>

        <hr class="hr15">
        <input value="登录" lay-submit lay-filter="login" style="width:100%;" type="submit">
        <hr class="hr20">
    </form>
    <button class="btn" onclick="window.location.href='${pageContext.request.contextPath}/login/byNamePage'">账户密码登录
    </button>
</div>

<script>

    $(function () {

        $('#verifyBtn').click(function () {
            if (!/^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/.test($("input[name=userPhone]").val())) {
                layer.msg("手机号码格式不正确！~");
                return;
            }
            $.ajax({
                type: "post",
                url: "${pageContext.request.contextPath}/login/sendVerifyCode",
                data: {userPhone: $("input[name=userPhone]").val()},
                dataType: "json",
                success: function (json) {
                    layer.msg(json.msg);
                    if (json.code === 200) {
                        let count = 60;
                        const countDown = setInterval(() => {
                            if (count === 0) {
                                $('#verifyBtn').text('重新发送').removeAttr('disabled');
                                $('#verifyBtn').css({
                                    background: '#1E9FFF',
                                    color: '#fff',
                                });
                                clearInterval(countDown);
                            } else {
                                $('#verifyBtn').attr('disabled', true);
                                $('#verifyBtn').css({
                                    background: '#d8d8d8',
                                    color: '#707070',
                                });
                                $('#verifyBtn').text(count + 's后重新获取');
                            }
                            count--;
                        }, 1000);
                    }
                }
            });

        });


        layui.use('form', function () {
            $ = layui.jquery;
            var form = layui.form
                , layer = layui.layer;


            //监听提交
            form.on('submit(login)', function (data) {
                // 提交数据到后台
                $.ajax({
                    type: "post",
                    url: "${pageContext.request.contextPath}/login/checkVerifyCode",
                    data: data.field,
                    dataType: "json",
                    success: function (json) {
                        if (json.code === 200) {
                            $.ajax({
                                type: "post",
                                url: "${pageContext.request.contextPath}/login/byPhone",
                                data: data.field,
                                dataType: "json",
                                success: function (json) {
                                    layer.msg(json.msg, {icon: json.code === 200 ? 6 : 5}, function () {
                                        if (json.code === 200) {
                                            location.href = '${pageContext.request.contextPath}/index/page';
                                        }
                                    });
                                }
                            });
                        }else {
                            layer.msg(json.msg);
                        }
                    }
                });
                return false;
            });
        });
    });


</script>


<!-- 底部结束 -->

</body>
</html>